package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.po.YgDrawPrize;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.WxMpAuthorityService;
import com.ygqh.baby.service.YgDrawPrizeService;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.HttpUtil;
import com.ygqh.baby.utils.ImageUtils;
import com.ygqh.baby.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/1/9 18:28
 * @since 1.0.0
 */
@RequestMapping("/mobile/nygift")
@Controller
public class NewYearGiftController {

    @Autowired
    private YgDrawPrizeService ygDrawPrizeService;
    @Autowired
    private FileService fileService;
    @Autowired
    private WxMpAuthorityService wxMpAuthorityService;
    @Value("${image.domain}")
    private String domain;

    private final static String KEY_PREFIX = "NY_GIFT_%s";

    @RequestMapping("/join")
    @ResponseBody
    public JSONPObject join(String callback, String sourceCode) {
        YgUser currentUser = SessionUtil.getCurrentUser();
        YgDrawPrize byUserId = ygDrawPrizeService.findByUserId(currentUser.getId(), sourceCode);
        if (byUserId != null) {
            return new JSONPObject(callback, Message.success(byUserId.getId()));
        }
         YgDrawPrize record = new YgDrawPrize();
        record.setUserId(currentUser.getId());
        Boolean newUser = DateConvertUtils.isToday(currentUser.getCreateTime());
        //0:新用户，1：老用户
        record.setRate(newUser ? BigDecimal.ZERO : BigDecimal.ONE);
        record.setSourceCode(sourceCode);
        //10代表中奖
        record.setCount(0);
        record.setCreateTime(new Date());
        record.setUpdateBy("新年有礼");
        record.setUpdateTime(new Date());
        record.setRemark(currentUser.getUserName());
        ygDrawPrizeService.save(record);
        return new JSONPObject(callback, Message.success(record.getId()));
    }

    @RequestMapping("/getPoster")
    @ResponseBody
    public JSONPObject getPoster(String callback, String source, HttpServletRequest request) throws Exception {
        String key = String.format(KEY_PREFIX, source);

        YgUser currentUser = SessionUtil.getCurrentUser();
        String codeUrl = wxMpAuthorityService.findQrCodeUrl(key);

        String shareCodeUrl = saveImageToTarget(request, codeUrl, key, currentUser.getUserName());
        return new JSONPObject(callback, Message.success(null));
    }

    private String saveImageToTarget(HttpServletRequest request, String codeUrl, String key, String source) throws Exception {
        String ext = ".jpg";
        String path = request.getRealPath("/");
        String targetPath = path + "/" + key;
        String ditu = HttpUtil.saveImageToTargetDisk(domain + "default/zhuawawa.jpg", targetPath + "/" + source + "_ditu" + ext);
        String shareCodeUrl = HttpUtil.saveImageToTargetDisk(codeUrl, targetPath + "/" + key + ext);

        ImageUtils.pressImageMark2(shareCodeUrl, ditu, shareCodeUrl, 100, 780, 1.0f, 180, 180);

        File file = new File(shareCodeUrl);

        shareCodeUrl = fileService.uploadImage(file, new FileType[]{FileType.QrCode}, ext, true, request);
        return shareCodeUrl;
    }


}
